using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace TALoaders
{
    /// <summary>To be used instead of "live" downloaders. It is returning deterministic data instead for live prices</summary>
    public class TestDownloader : BaseLoader
    {
        #region Private members
        /// <summary>OMX prices from OMX(2009-08-20)=886.46 ; to OMX(2010-06-03)=1005.93 </summary>
        private double[] hPricesM = {886.46, 913.84, 921.63, 916.06,902.79,901.95,919.91,904.84,888.5,873.35,879.28,891.39,903.62,905.68,921.48,916.53,918.19,914.31,913.07,918.44,923.82,920.23,913.05,922.24,922.99,901.87,899.87,913.55,902.82,896.76,878.33,867.84,876.43,891.88,885.43,895.35,899.7,910.79,903.03,928.59,927.05,927.76,941.67,942.67,940.22,927.47,932.29,918.24,929.54,924.27,939.82,944.67,932.06,919.27,937.42,944.22,941.48,960.46,957.98,964.55,970.36,967.44,975.01,964.52,965.88,951.44,951.57,975.47,965.54,971.42,939.68,952.53,936.19,953.88,957.1,954.64,966.97,966.14,953.48,944.95,947.33,949.84,956.25,949.07,959.6,946.58,936.03,948.38,961.88,961.54,965.05,965.8,951.72,963.56,967.27,965.82,973.44,978.18,969.55,974.96,981.5,970.65,974.05,976,957.53,954.02,951.06,947.56,942.94,946.7,937.22,953.71,967.77,971.8,970,948.62,935.89,938.42,944.8,937.05,943.66,931.35,923.37,934.31,943.69,946.97,957.27,954.1,942.05,944.31,940.51,947.39,966.38,971.67,981.19,984.33,998.07,1003.39,1000.5,1013.04,1008.93,1018.15,1013.51,1020.05,1025.58,1018.19,1020.26,1021.42,1030.13,1027.08,1033.34,1027.42,1026.7,1022.79,1021.08,1036.93,1046,1042.78,1036.73,1044.58,1048.27,1046.5,1051.76,1053.25,1050.24,1043.04,1055.34,1042.48,1038.34,1066.82,1067.9,1042.82,1042.87,1061.77,1053.88,1059.44,1027.5,1000.7,977.25,944.57,1005.34,993.04,1010.25,984.91,991.58,1000.41,969.06,947.93,953.77,953.54,931.48,957.05,978.55,979.12,
                    980.63, 981.25, 989.77, 1005.93};
        private double lastPriceM = 988.46;
        private TraceSwitch traceM = new TraceSwitch("TestLoader", "Debug messages in TestLoader");
        #endregion

        #region Public Methods
        public TestDownloader()
        : base(Loader.Test)
        { }

        override public SortedList<DateTime, Quote> getHistoricalPrices(string ticker, DateTime startDate, DateTime endDate)
        {
            Debug.WriteLineIf(traceM.TraceInfo, "TestDownloader.getHistoricalPrices(" + ticker + ", " + startDate.ToShortDateString() + ", " + endDate.ToShortDateString() + ")");
            Debug.Indent();
            SortedList<DateTime, Quote> result = new SortedList<DateTime, Quote>();
            int n = hPricesM.Length;
            int i = 0;
            for (DateTime d = endDate; d >= startDate; d = d.AddDays(-1), ++i)
            {
                double open = hPricesM[i < n ? n - i - 1 : 0];
                double low = open * (1 - i%3 *0.01);
                double high = open * (1 + i%5 * 0.01);
                double close = open;
                double volume = 100;
                double adjClose = open;
                Quote quote = new Quote(open, high, low, close, volume, adjClose);
                result.Add(d, quote);
                Debug.WriteLineIf(traceM.TraceVerbose, d.ToShortDateString() + "," + quote.ToString());
            }
            Debug.Unindent();
            return result;
        }

        override public double getLastPrice(string ticker, out string timeStamp)
        {
            timeStamp = "12:34";
            return lastPriceM;
        }
        #endregion
    }
}
